
package management;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Category extends JFrame{
    public Connection connection;

    public void csql() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.err.println("加载错误");
        }
        String username = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/project";
        try {
            connection = DriverManager.getConnection(
                    url,
                    username,
                    password
            );
        } catch (
                SQLException throwables) {
            throwables.printStackTrace();
            System.err.println("数据库在连接期间，出现错误！");
        }
    }

    public List<String> getCategories() {
        List<String> categoryList = new ArrayList<>();
        csql();
        String sql = "SELECT * FROM consumption_category ";
        try (
                Statement stmt = connection.createStatement();
                ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                String categoryName = rs.getString("category_name");
                categoryList.add(categoryName);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return categoryList;
    }

    public void addCategory(String catg) {
        String sql = "INSERT INTO  consumption_category (category_name) VALUES (?)";
        try (
                PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, catg);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void deleteCategory(String catg) {

        String sql = "DELETE  from consumption_category where category_name=(?)";

        try (
                PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, catg);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


        private JTextField categoryField;
        private JButton addButton;

        public void AddCategoryWindow() {
            setTitle("添加消费类别");
            setBounds(400, 300, 380, 246);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setLayout(null);

            JLabel categoryLabel = new JLabel("消费类别:");
            categoryLabel.setBounds(60, 50, 150, 20);
            categoryField = new JTextField();
            categoryField.setBounds(120, 50, 150, 20);
            addButton = new JButton("添加");
            addButton.setBounds(140, 140, 80, 25);

            add(categoryLabel);
            add(categoryField);
            add(addButton);

            addButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String categoryName = categoryField.getText();
                    csql();
                    addCategory(categoryName);
                    if (!categoryName.isEmpty()) {
                        JOptionPane.showMessageDialog(null, "消费类别添加成功");

                    } else {
                        JOptionPane.showMessageDialog(null, "请输入消费类别名称");
                    }
                }
            });

            setVisible(true);
        }

        private JComboBox<String> categoryComboBox;
        private JButton deleteButton;

        public void DeleteCategoryWindow() {
            setTitle("删除消费类别");
            setBounds(400, 300, 380, 246);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setLayout(null);
            JLabel categoryLabel = new JLabel("选择要删除的消费类别:");
            categoryLabel.setBounds(30, 30, 150, 20);
            categoryComboBox = new JComboBox<>();
            categoryComboBox.setBounds(170, 30, 150, 20);
            List<String> list = new ArrayList<>();
            Category category =new Category();
            list = category.getCategories();
            for (int i = 0; i < list.size(); i++) {
                categoryComboBox.addItem(list.get(i));
            }


            deleteButton = new JButton("删除");
            deleteButton.setBounds(110,100,120,30);

            add(categoryLabel);
            add(categoryComboBox);
            add(deleteButton);


            deleteButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    csql();
                    String selectedCategory = (String) categoryComboBox.getSelectedItem();
                    deleteCategory(selectedCategory);
                    if (selectedCategory != null) {

                        JOptionPane.showMessageDialog(null, "消费类别删除成功");
                    } else {
                        JOptionPane.showMessageDialog(null, "请选择要删除的消费类别");
                    }
                }
            });


            setVisible(true);
        }






    public static void main(String[] args) {

    Category category =new Category();
    //category.DeleteCategoryWindow();
    category.AddCategoryWindow();
    }
}
